//
//  FavMerchantParser.m
//  FootPrint
//
//  Created by 秦 东方 on 4/11/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "FavMerchantParser.h"
#import "UserFavoriteMerchantQueryRequest.pb.h"
#import "CustomedMerchantRequest.pb.h"
#import "UserFavoriteMerchantRequest.pb.h"
#import "UserFavoriteMerchantDelRequest.pb.h"

@implementation FavMerchantParser

- (void) dealloc{
    _delegate = nil;
    [super dealloc];
}

- (void) reqFavMerchant : (long long)uid{
    if(uid <= 0){
        [_delegate failReqFav];
        return;
    }
    
    UserFavoriteMerchantQueryRequestMessage_Builder* builder = [UserFavoriteMerchantQueryRequestMessage builder];
    [builder setUserId:uid];
    UserFavoriteMerchantQueryRequestMessage* msg = [builder build];
    NSData* queryData = [msg data];
    
    if(queryData){
        
        [super noCacheRequestURL:URL_FAVORITE_MERCHANT postData:queryData completion:^(NSData* resultData){
            @try {
                UserFavoriteMerchantQueryResponseMessage* resp = [UserFavoriteMerchantQueryResponseMessage parseFromData:resultData];
                [_delegate didFinishReqFav:resp];
            }
            @catch (NSException *exception) {
                 [_delegate failReqFav];
            }
        
        } fail:^(NSError * err) {
            [_delegate failReqFav];
        }];
        
    } else {
        [_delegate failReqFav];
    }
    
}


- (void)reqAddMerchant:(long long)uid name:(NSString*)name address:(NSString*)addr{
    
    MerchantMessage* mer = [[[[[MerchantMessage builder] setId:0] setName:name] setAddress:addr] build];
    
    CustomizedMerchantRequestMessage_Builder* builder = [CustomizedMerchantRequestMessage builder];
    [builder setUserId:uid];
    [builder setMerchant:mer];
    
    NSData* queryData = [[builder build] data];
    if(queryData){
        [super noCacheRequestURL:URL_UEER_MERCHANT postData:queryData completion:^(NSData* resultData){
            CustomizedMerchantResponseMessage* resp = [CustomizedMerchantResponseMessage parseFromData:resultData];
            [_delegate didFinishAddMer:resp];
        } fail:^(NSError * err) {
            [_delegate failAddMer];
        }];
    } else {
        [_delegate failAddMer];
    }
}

- (void)reqAddFav:(long long)uid merId:(long long)merId{
    NSData* queryData = [[[[[UserFavoriteMerchantRequestMessage builder] setUserId:uid] setMerchantId:merId] build] data];
    if(queryData){
        [super noCacheRequestURL:URL_ADD_FAVORITE postData:queryData completion:^(NSData* resultData){
            UserFavoriteMerchantResponseMessage* resp = [UserFavoriteMerchantResponseMessage parseFromData:resultData];
            [_delegate didFinishAddFav:resp];
        } fail:^(NSError * err) {
            [_delegate failReqFav];
        }];
    } else {
        [_delegate failReqFav];
    }
}


- (void)reqDelFav:(long long)uid merId:(long long)merId{
    NSData* queryData = [[[[[UserFavoriteMerchantDelRequestMessage builder] setUserId:uid] setMerchantId:merId] build] data];
    if(queryData){
        [super noCacheRequestURL:URL_DEL_FAVORITE postData:queryData completion:^(NSData* resultData){
            UserFavoriteMerchantDelResponseMessage* resp = [UserFavoriteMerchantDelResponseMessage parseFromData:resultData];
            [_delegate didFinishDel:resp];
        } fail:^(NSError * err) {
            [_delegate failDelFav];
        }];
    } else {
        [_delegate failDelFav];
    }
}

@end
